﻿Public Class Form1

    Public player1Name As String
    Public player2Name As String
    Public numPlayers As Integer
    Public shipondisplay As Ship
    Private pictureBoxen As New List(Of PictureBox)
    'ship on display needs to be modified so "New Game" in the middle of game doesnt crash
    Dim WithEvents CurrentGameControl As New BattleshipGameControl


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        frmInstruct.ShowDialog()
    End Sub
    Private Sub NewGameToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewGameToolStripMenuItem.Click
        EndGame()
        frmNewGame.ShowDialog()
        CurrentGameControl.ManageTurnsAndPhases(0)
    End Sub
    Public Sub ValidatePlayerName()
        CurrentGameControl.PlayerNameInput(player1Name)
        CurrentGameControl.CurrentGame.Player1Name = player1Name

        If numPlayers = 2 Then
            CurrentGameControl.PlayerNameInput(player2Name)
            CurrentGameControl.CurrentGame.Player2Name = player2Name
        End If

        CurrentGameControl.CurrentGame.NumPlayers = numPlayers
    End Sub
    Sub SetNames() Handles CurrentGameControl.SetNames
        CurrentGameControl.CurrentGame.Player1Name = player1Name
        CurrentGameControl.CurrentGame.Player2Name = player2Name
    End Sub
    Sub HotSeat() Handles CurrentGameControl.HotSeatBlackOut
        Me.Hide()
        frmBlackout.ShowDialog()
        Me.Show()
    End Sub
    Sub ClearPictureBoxes() Handles CurrentGameControl.ClearPB
        If pictureBoxen.Count <> 0 Then
            For Each i As PictureBox In pictureBoxen
                i.Image = Nothing
            Next
        End If
    End Sub
    Sub EnableInteractivity() Handles CurrentGameControl.EnableInteractivity
        For Each k As Windows.Forms.Control In Me.Controls
            k.Enabled = True
        Next
    End Sub
    Sub DisableInteractivity() Handles CurrentGameControl.DisableInteractivity
        For Each k As Windows.Forms.Control In Me.Controls
            k.Enabled = False
        Next
    End Sub
    Sub DisableGroupBox() Handles CurrentGameControl.DisableShipPlacementUI
        GroupBox3.Enabled = False
        GroupBox3.Visible = False
        ListBox1.Left = 20
        ListBox1.Width = (ListBox1.Width + 200)
    End Sub
    Private Sub EditToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditToolStripMenuItem.Click
        frmInstruct.ShowDialog()
    End Sub
    Public Sub gridclick(ByVal sender As Windows.Forms.PictureBox, ByVal e As System.EventArgs) Handles pb_h5.Click, pb_a1.Click, PictureBox98.Click, PictureBox97.Click, PictureBox96.Click, PictureBox95.Click, PictureBox94.Click, PictureBox93.Click, PictureBox92.Click, PictureBox91.Click, PictureBox90.Click, PictureBox9.Click, PictureBox89.Click, PictureBox88.Click, PictureBox87.Click, PictureBox86.Click, PictureBox85.Click, PictureBox84.Click, PictureBox83.Click, PictureBox82.Click, PictureBox81.Click, PictureBox80.Click, PictureBox8.Click, PictureBox78.Click, PictureBox77.Click, PictureBox76.Click, PictureBox75.Click, PictureBox74.Click, PictureBox73.Click, PictureBox72.Click, PictureBox71.Click, PictureBox70.Click, PictureBox7.Click, PictureBox69.Click, PictureBox68.Click, PictureBox67.Click, PictureBox66.Click, PictureBox65.Click, PictureBox64.Click, PictureBox63.Click, PictureBox62.Click, PictureBox61.Click, PictureBox60.Click, PictureBox6.Click, PictureBox59.Click, PictureBox58.Click, PictureBox57.Click, PictureBox56.Click, PictureBox55.Click, PictureBox54.Click, PictureBox53.Click, PictureBox52.Click, PictureBox51.Click, PictureBox50.Click, PictureBox49.Click, PictureBox48.Click, PictureBox47.Click, PictureBox46.Click, PictureBox44.Click, PictureBox43.Click, PictureBox42.Click, PictureBox41.Click, PictureBox40.Click, PictureBox4.Click, PictureBox39.Click, PictureBox38.Click, PictureBox37.Click, PictureBox36.Click, PictureBox35.Click, PictureBox34.Click, PictureBox33.Click, PictureBox32.Click, PictureBox31.Click, PictureBox30.Click, PictureBox3.Click, PictureBox29.Click, PictureBox28.Click, PictureBox27.Click, PictureBox26.Click, PictureBox25.Click, PictureBox24.Click, PictureBox22.Click, PictureBox21.Click, PictureBox20.Click, PictureBox2.Click, PictureBox19.Click, PictureBox18.Click, PictureBox17.Click, PictureBox16.Click, PictureBox15.Click, PictureBox14.Click, PictureBox13.Click, PictureBox12.Click, PictureBox11.Click, PictureBox10.Click, PictureBox1.Click, pb_two.Click, pb_g4.Click, pb_g2.Click, PictureBox23.Click, PictureBox200.Click, PictureBox199.Click, PictureBox198.Click, PictureBox197.Click, PictureBox196.Click, PictureBox195.Click, PictureBox194.Click, PictureBox193.Click, PictureBox192.Click, PictureBox191.Click, PictureBox190.Click, PictureBox189.Click, PictureBox188.Click, PictureBox187.Click, PictureBox186.Click, PictureBox185.Click, PictureBox184.Click, PictureBox183.Click, PictureBox182.Click, PictureBox181.Click, PictureBox180.Click, PictureBox179.Click, PictureBox178.Click, PictureBox177.Click, PictureBox176.Click, PictureBox175.Click, PictureBox174.Click, PictureBox173.Click, PictureBox172.Click, PictureBox171.Click, PictureBox170.Click, PictureBox169.Click, PictureBox168.Click, PictureBox167.Click, PictureBox166.Click, PictureBox165.Click, PictureBox164.Click, PictureBox163.Click, PictureBox162.Click, PictureBox161.Click, PictureBox160.Click, PictureBox159.Click, PictureBox158.Click, PictureBox157.Click, PictureBox156.Click, PictureBox155.Click, PictureBox154.Click, PictureBox153.Click, PictureBox152.Click, PictureBox151.Click, PictureBox150.Click, PictureBox149.Click, PictureBox148.Click, PictureBox147.Click, PictureBox146.Click, PictureBox145.Click, PictureBox144.Click, PictureBox143.Click, PictureBox142.Click, PictureBox141.Click, PictureBox140.Click, PictureBox139.Click, PictureBox138.Click, PictureBox137.Click, PictureBox136.Click, PictureBox135.Click, PictureBox134.Click, PictureBox133.Click, PictureBox132.Click, PictureBox131.Click, PictureBox130.Click, PictureBox129.Click, PictureBox128.Click, PictureBox127.Click, PictureBox126.Click, PictureBox125.Click, PictureBox124.Click, PictureBox123.Click, PictureBox122.Click, PictureBox121.Click, PictureBox120.Click, PictureBox119.Click, PictureBox118.Click, PictureBox117.Click, PictureBox116.Click, PictureBox115.Click, PictureBox114.Click, PictureBox113.Click, PictureBox112.Click, PictureBox111.Click, PictureBox110.Click, PictureBox109.Click, PictureBox108.Click, PictureBox107.Click, PictureBox106.Click, PictureBox105.Click, PictureBox104.Click, PictureBox103.Click, PictureBox102.Click, PictureBox101.Click
        Dim tabletohandle As TableLayoutPanel = sender.Parent
        Dim horizontalvertical As Integer = 0

        If rb_shiphorizontal.Checked = True Then 'its horizontal placement time
            horizontalvertical = 1
        End If

        If rb_vertical.Checked = True Then 'its vertical placement time
            horizontalvertical = 2
        End If

        If sender.Parent.Name = "table1" Then 'it's a firing grid click
            Dim col As Integer = table1.GetColumn(sender)
            Dim row As Integer = table1.GetRow(sender)
            Dim gridclicked As Integer = 1
            CurrentGameControl.HandleClickEvent(col, row, gridclicked, horizontalvertical)
        End If

        If sender.Parent.Name = "table2" Then 'it's a tracking grid click
            Dim col As Integer = table2.GetColumn(sender)
            Dim row As Integer = table2.GetRow(sender)
            Dim gridclicked As Integer = 2
            CurrentGameControl.HandleClickEvent(col, row, gridclicked, horizontalvertical)

        End If

        'If sender.Parent.Name Anything Else, don't bother doing anything. Those are non-interactive.

    End Sub
    Public Sub UpdatePlayerListboxUI(ByRef playerfeedback As String) Handles CurrentGameControl.UpdateTextOutput
        If playerfeedback = "clear" Then
            ListBox1.Items.Clear()
        Else
            ListBox1.Items.Add(playerfeedback)
            ListBox1.SelectedIndex = ListBox1.Items.Count - 1
        End If

    End Sub

    Public Sub EndGame() Handles CurrentGameControl.EndGameEvent
        UpdatePlayerListboxUI("clear")
        shipondisplay = Nothing
        numPlayers = Nothing
        player1Name = ""
        player2Name = ""
        CurrentGameControl = Nothing
        CurrentGameControl = New BattleshipGameControl
        CurrentGameControl.ManageTurnsAndPhases(0)
    End Sub

    Public Sub DrawAGrid(ByVal TypeOfGridToDraw As Integer, ByVal GridToDraw As BattleshipGameGrid, Optional ByVal shipforlayergrid As Ship = Nothing) Handles CurrentGameControl.OutputGridToDraw

        Select Case TypeOfGridToDraw

            Case 1 ' Draw a Firing Grid

                'the firing grid is always the left grid, aka, table1
                Dim GridToDrawTo As TableLayoutPanel = table1

                '0th, make then all blue (I did this in the UI, but it may need to be called if a square gets un-empty as players switch, etc.)
                '0.5th, make sure that one cannot see the last player's grids!
                For Each i As Windows.Forms.Control In GridToDrawTo.Controls
                    If i.GetType.Name = "PictureBox" Then
                        i.BackColor = Color.DeepSkyBlue
                        i.BackgroundImage = Nothing

                    End If
                Next

                For Each i As Windows.Forms.Control In GridToDrawTo.Controls
                    If i.GetType.Name = "PictureBox" Then
                        If GridToDraw.PlayerGrid(GridToDrawTo.GetColumn(i), GridToDrawTo.GetRow(i)).ShipExists Then
                            i.BackgroundImage = GridToDraw.PlayerGrid(GridToDrawTo.GetColumn(i), GridToDrawTo.GetRow(i)).ShipSprite
                        End If

                    End If
                Next

                'Then, write the pegs on top of the ships if they belong there.
                For Each i As Windows.Forms.Control In GridToDrawTo.Controls
                    If i.GetType.Name = "PictureBox" Then
                        Dim drawthispb As PictureBox = i
                        If GridToDraw.PlayerGrid(GridToDrawTo.GetColumn(drawthispb), GridToDrawTo.GetRow(drawthispb)).ShotState = 1 Then
                            pictureBoxen.Add(drawthispb)
                            drawthispb.Image = My.Resources.whitepeg
                        End If

                        If GridToDraw.PlayerGrid(GridToDrawTo.GetColumn(drawthispb), GridToDrawTo.GetRow(drawthispb)).ShotState = 2 Then
                            pictureBoxen.Add(drawthispb)
                            drawthispb.Image = My.Resources.redpeg
                        End If
                    End If
                Next


            Case 2 'Draw A Tracking Grid

                Dim GridToDrawTo As TableLayoutPanel = table2

                For Each i As Windows.Forms.Control In GridToDrawTo.Controls
                    If i.GetType.Name = "PictureBox" Then
                        Dim necessaryabstraction As PictureBox = i

                        If GridToDraw.PlayerGrid(GridToDrawTo.GetColumn(necessaryabstraction), GridToDrawTo.GetRow(necessaryabstraction)).ShotState = 1 Then
                            pictureBoxen.Add(necessaryabstraction)
                            necessaryabstraction.Image = My.Resources.whitepeg
                        End If


                        If GridToDraw.PlayerGrid(GridToDrawTo.GetColumn(necessaryabstraction), GridToDrawTo.GetRow(necessaryabstraction)).ShotState = 2 Then
                            pictureBoxen.Add(necessaryabstraction)
                            necessaryabstraction.Image = My.Resources.redpeg
                        End If
                    End If
                Next


            Case 3 ' Draw a ShipPlacementGrid
                'have to keep a copy of this ship in case it needs to be flipped

                Me.ShowOffAShip(shipforlayergrid)
        End Select





    End Sub
    Private Sub StatisticsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StatisticsToolStripMenuItem.Click
        frmStats.ShowDialog()
    End Sub
    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub
    Sub ShowOffAShip(ByRef shipinquestion As Ship)

        'gotta keep track of that in case it needs to be re-drawn in a different orientation
        shipondisplay = shipinquestion

        'First, get rid of anything else that was up in here.

        a1.BackgroundImage = Nothing
        a2.BackgroundImage = Nothing
        a3.BackgroundImage = Nothing
        a4.BackgroundImage = Nothing
        a5.BackgroundImage = Nothing

        b1.BackgroundImage = Nothing
        c1.BackgroundImage = Nothing
        d1.BackgroundImage = Nothing
        e1.BackgroundImage = Nothing


        If rb_shiphorizontal.Checked = True Then

            If shipinquestion.Size = 5 Then

                a1.BackgroundImage = shipinquestion.HorizontalSprites(1)
                a2.BackgroundImage = shipinquestion.HorizontalSprites(2)
                a3.BackgroundImage = shipinquestion.HorizontalSprites(3)
                a4.BackgroundImage = shipinquestion.HorizontalSprites(4)
                a5.BackgroundImage = shipinquestion.HorizontalSprites(5)
            End If

            If shipinquestion.Size = 4 Then

                a1.BackgroundImage = shipinquestion.HorizontalSprites(1)
                a2.BackgroundImage = shipinquestion.HorizontalSprites(2)
                a3.BackgroundImage = shipinquestion.HorizontalSprites(3)
                a4.BackgroundImage = shipinquestion.HorizontalSprites(4)

            End If

            If shipinquestion.Size = 3 Then

                a1.BackgroundImage = shipinquestion.HorizontalSprites(1)
                a2.BackgroundImage = shipinquestion.HorizontalSprites(2)
                a3.BackgroundImage = shipinquestion.HorizontalSprites(3)

            End If

            If shipinquestion.Size = 2 Then

                a1.BackgroundImage = shipinquestion.HorizontalSprites(1)
                a2.BackgroundImage = shipinquestion.HorizontalSprites(2)

            End If

        End If

        If rb_vertical.Checked = True Then

            If shipinquestion.Size = 5 Then
                a1.BackgroundImage = shipinquestion.VerticalSprites(1)
                b1.BackgroundImage = shipinquestion.VerticalSprites(2)
                c1.BackgroundImage = shipinquestion.VerticalSprites(3)
                d1.BackgroundImage = shipinquestion.VerticalSprites(4)
                e1.BackgroundImage = shipinquestion.VerticalSprites(5)

            End If



            If shipinquestion.Size = 4 Then
                a1.BackgroundImage = shipinquestion.VerticalSprites(1)
                b1.BackgroundImage = shipinquestion.VerticalSprites(2)
                c1.BackgroundImage = shipinquestion.VerticalSprites(3)
                d1.BackgroundImage = shipinquestion.VerticalSprites(4)
            End If

            If shipinquestion.Size = 3 Then
                a1.BackgroundImage = shipinquestion.VerticalSprites(1)
                b1.BackgroundImage = shipinquestion.VerticalSprites(2)
                c1.BackgroundImage = shipinquestion.VerticalSprites(3)

            End If

            If shipinquestion.Size = 2 Then
                a1.BackgroundImage = shipinquestion.VerticalSprites(1)
                b1.BackgroundImage = shipinquestion.VerticalSprites(2)
            End If

        End If
    End Sub
    Private Sub rb_vertical_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rb_vertical.CheckedChanged
        ShowOffAShip(shipondisplay)
    End Sub
End Class